<?php

class CommentMapper extends AMapper
{
	protected static $self = NULL;
	public static function &get() { return parent::_get(__CLASS__); }
	protected static function &getSelf() { return self::$self; }
	protected static function setSelf($in_self) { self::$self = $in_self; }

	public function find($in_comment_id)
	{
		$commentdata = DBConn::selectRow(sprintf("SELECT * FROM comments WHERE id=%d;",$in_comment_id));
		if(!$commentdata || empty($commentdata))
		{
			throw new FrameworkRecordNotFoundException("comment");
		}
		return $this->load($commentdata);
	}

	public function findByPost($in_post)
	{
		if(!$in_post instanceOf Post)
			throw new FrameworkInvalidTypeException('findByPost', 'Post', get_class($in_post));

		$commentdata = DBConn::selectAll(sprintf("SELECT * FROM comments WHERE post_id=%d ORDER BY date_added ASC", $in_post->getId()));
		$comments = array();
		foreach($commentdata as $commentrow)
		{
			$comments[] = $this->load($commentrow);
		}
		return $comments;
	}

	public function insert($in_comment)
	{
		return $this->automaticInsert($in_comment);
	}

	public function update($in_comment)
	{
		return $this->automaticUpdate($in_comment);
	}

	public function findNumByPost($in_post)
	{
		$query = "SELECT count(id) FROM comments WHERE post_id=%d";
		$num = DBConn::selectOne(sprintf($query, $in_post->getId()));
		return $num;
	}

	public function &load($in_comment_array)
	{
		$entity = substr(__CLASS__,0,-6);
		$comment = new $entity($in_comment_array);
		return $comment;
	}
}

?>
